/**
 *
 */
package com.oceansoft.mobile.econsole.modules.advice.controller;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.oceansoft.mobile.econsole.modules.advice.entity.Advice;
import com.oceansoft.mobile.econsole.modules.advice.service.IAdviceService;
import com.oceansoft.mobile.econsole.modules.apps.entity.AppDownload;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.channel.entity.Channel;
import com.oceansoft.mobile.econsole.modules.log.entity.AccessLog;
import com.oceansoft.mobile.econsole.modules.log.service.IAccessLogService;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 意见投诉
 *
 * @author jin.xu
 * @version 2014-08-06
 */
@Controller
@RequestMapping("advice")
public class AdviceController extends BaseController {

    @Resource
    private IAdviceService adviceService;
    
  

    /**
     * 查看意见投诉明细
     *
     * @param adviceId
     * @param model
     * @return
     */
    @RequestMapping(value = "view/{adviceId}", method = GET)
    public String loadAdviceById(@PathVariable("adviceId") int adviceId, Model model) {
        model.addAttribute("advice", adviceService.findAdviceById(adviceId));
        return "advice/advice-view";
    }
    
    @RequestMapping(value ="reply/{adviceId}/{adviceHandler}")
    public String replyAdvicById(@PathVariable("adviceId") int adviceId,@PathVariable("adviceHandler") int num,Model model){
    	model.addAttribute("advice", adviceService.findAdviceById(adviceId));
    	
    	if(num==1){
    		
    	return "advice/advice-reply";
    	}else{
    		return "advice/advice-show";
    	}
    	
    }
    
    @ResponseBody
    @RequestMapping(value="send/{sumbitHandler}")
    public Result sendMessage(@PathVariable("sumbitHandler") int sumbitHandler,Advice advice){
    	if(sumbitHandler==1){
    	return adviceService.pushBackInfo(advice);
    	}
    	else
    	{
    		return adviceService.updateStatus(advice);
    		
    	}
    	
    }
    
    

    
    
    

    /**
     * 分页查看投诉意见
     *
     * @param query
     * @param model
     * @return
     */
    @RequestMapping(value = "list")
    public String loadAdviceByPage(Query query, Model model) {
        Pager pager = adviceService.findAdviceByPage(query);
        model.addAttribute("query", query).addAttribute("pager", pager);
        log(String.format("查看反馈意见列表第%d页", query.getPageNum()));
        return "advice/advice-list";
    }
//    @RequestMapping(value = "list", method = POST)
//    public String loadPageByCondition(Query query, Model model) {
//    	return loadAdviceByPage( query,  model);
//}
    /**
     * 统计量导出
     *
     * @return
     */
    @RequestMapping("exportExcel.xls")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Query query) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      //  String newdate = sdf.format(new Date());
        // 第一步，创建一个webbook，对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("内容反馈");  
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
  
        HSSFCell cell = row.createCell((short)0);
        cell.setCellValue("序号");
        cell.setCellStyle(style);
        cell = row.createCell((short)1);
        cell.setCellValue("登陆账号");
        cell.setCellStyle(style);  
        cell = row.createCell((short)2);  
        cell.setCellValue("联系方式");
        cell.setCellStyle(style);  
        cell = row.createCell((short)3);  
        cell.setCellValue("反馈时间");
        cell.setCellStyle(style);  
        cell = row.createCell((short)4);  
        cell.setCellValue("手机机型");
        cell.setCellStyle(style);  
        cell = row.createCell((short)4);  
        cell.setCellValue("反馈意见");  
        cell.setCellStyle(style); 
  
        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
        List<Advice> list= adviceService.loadExcel(query); 
        for (int i = 0; i < list.size(); i++)  
        {  
            row = sheet.createRow((int) i + 1);  
           Advice advice = list.get(i);  
            // 第四步，创建单元格，并设置值  
           row.createCell((short)0).setCellValue(advice.getId());  
           row.createCell((short)1).setCellValue(advice.getLoginId()); 
           row.createCell((short)1).setCellValue(advice.getContact()); 
            row.createCell((short)2).setCellValue(sdf.format(advice.getCreateTime()));              
            row.createCell((short)3).setCellValue(advice.getMobileType());  
            row.createCell((short)4).setCellValue(advice.getContent());  
        }  
        // 第六步，将文件存到指定位置  
        try  
        {
        	response.setContentType("application/ms-excel");
        	OutputStream out = response.getOutputStream();
        	wb.write(out);  
            out.close();  
        }
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }
    

    
}
